package com.example.management.common.controller

import com.example.management.common.config.BootdoConfig
import com.example.management.common.domain.FileDO
import com.example.management.common.service.FileService
import com.example.management.common.utils.*
import org.apache.shiro.authz.annotation.RequiresPermissions
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
import springfox.documentation.annotations.ApiIgnore
import java.util.*
import javax.servlet.http.HttpServletRequest

@ApiIgnore
@Controller
@RequestMapping("/common/sysFile")
class FileController : BaseController() {

    @Autowired
    private val sysFileService: FileService? = null

    @Autowired
    private val bootdoConfig: BootdoConfig? = null

    @GetMapping
    @RequiresPermissions("common:sysFile:sysFile")
    internal fun sysFile(model: Model): String {
        val params = HashMap<String, Any>(16)
        return "common/file/file"
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("common:sysFile:sysFile")
    fun list(@RequestParam params: Map<String, Any>): PageUtils {
        // 查询列表数据
        val query = Query(params)
        val sysFileList = sysFileService!!.list(query)
        val total = sysFileService!!.count(query)
        return PageUtils(sysFileList, total)
    }

    @GetMapping("/add")
    internal fun add():
    // @RequiresPermissions("common:bComments")
            String {
        return "common/sysFile/add"
    }

    @GetMapping("/edit")
    internal fun edit(id: Long?, model: Model):
    // @RequiresPermissions("common:bComments")
            String {
        val sysFile = sysFileService!!.get(id)
        model.addAttribute("sysFile", sysFile)
        return "common/sysFile/edit"
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("common:info")
    fun info(@PathVariable("id") id: Long?): R {
        val sysFile = sysFileService!!.get(id)
        return R.ok().put("sysFile", sysFile)!!
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("common:save")
    fun save(sysFile: FileDO): R {
        return if (sysFileService!!.save(sysFile) > 0) {
            R.ok()
        } else R.error()
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("common:update")
    fun update(@RequestBody sysFile: FileDO): R {
        sysFileService!!.update(sysFile)

        return R.ok()
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    // @RequiresPermissions("common:remove")
    fun remove(id: Long?, request: HttpServletRequest): R {
        if ("test" == getUsername()) {
            return R.error(1, "演示系统不允许修改,完整体验请部署程序")
        }
        val fileName = bootdoConfig!!.uploadPath + sysFileService!!.get(id).url?.replace("/files/", "")
        if (sysFileService!!.remove(id) > 0) {
            val b = FileUtil.deleteFile(fileName)
            return if (!b) {
                R.error("数据库记录删除成功，文件删除失败")
            } else R.ok()
        } else {
            return R.error()
        }
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("common:remove")
    fun remove(@RequestParam("ids[]") ids: Array<Long>): R {
        if ("test" == getUsername()) {
            return R.error(1, "演示系统不允许修改,完整体验请部署程序")
        }
        sysFileService!!.batchRemove(ids)
        return R.ok()
    }

    @ResponseBody
    @PostMapping("/upload")
    internal fun upload(@RequestParam("file") file: MultipartFile, request: HttpServletRequest): R {
        if ("test" == getUsername()) {
            return R.error(1, "演示系统不允许修改,完整体验请部署程序")
        }
        var fileName = file.originalFilename
        fileName = FileUtil.renameToUUID(fileName)
        val sysFile = FileDO(FileType.fileType(fileName), "/files/" + fileName!!, Date())
        try {
            FileUtil.uploadFile(file.bytes, bootdoConfig!!.uploadPath, fileName)
        } catch (e: Exception) {
            return R.error()
        }

        return if (sysFileService!!.save(sysFile) > 0) {
            R.ok().put("fileName", sysFile.url!! )!!
        } else R.error()
    }


}
